package com.wenan.ui.controls.dateChooser
{
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.collections.XMLListCollection;
	import mx.formatters.DateFormatter;

	public class DateUtil
	{
		public function DateUtil()
		{
		}
		
		public static function formatDate(date:Date):String
		{
			var dataFormatter:mx.formatters.DateFormatter = new mx.formatters.DateFormatter();
			dataFormatter.formatString = "YYYY-MM-DD";
			if (date == null)
			{
				return "";
			}
			return dataFormatter.format(date);
		}
		
		public static function convert(index:int):String 
		{
			var month:Array = ["一月", "二月", "三月", "四月", 
				"五月", "六月", "七月", "八月",
				"九月", "十月", "十一月", "十二月"];
			return month[index - 1];
		}
		
		public static function getIndexByName(monthName:String):int 
		{
			var month:Array = ["一月", "二月", "三月", "四月", 
				"五月", "六月", "七月", "八月",
				"九月", "十月", "十一月", "十二月"];
			return month.indexOf(monthName, 0);;
		}
		
		public static function getItemList(value:Object):IList
		{
			var itemList:IList = null;
			
			if (value is Array)
			{
				itemList = new ArrayCollection(value as Array);
			}
			else if (value is IList)
			{
				itemList = value as IList;
			}
			else if (value is XMLList)
			{
				itemList = new XMLListCollection(value as XMLList);
			}
			else if (value is XML)
			{
				var xl:XMLList = new XMLList();
				xl += value;
				itemList = new XMLListCollection(xl);
			}
			else
			{
				// convert it to an array containing this one item
				var tmp:Array = [];
				if (value != null)
					tmp.push(value);
				itemList = new ArrayCollection(tmp);
			}

			return itemList;
		}
		
		/**
		 * 格式化时间，用户请求所用的参数
		 * */
		public static function formatDateToString(type:String, year:Number=NaN, month:Number=NaN, fromDay:Number=NaN, endDay:Number=NaN):String
		{
			var date:Date;
			var end:Number;
			if (type == "year")
			{
				date = new Date(year, 12);
				end = date.dateUTC;
				return year + "-" + 1 + "-" + 1 + "," + year + "-" + 12 + "-" + end;
			} else if (type == "month")
			{
				date = new Date(year, month);
				end = date.dateUTC;
				return year + "-" + numToString(month) + "-" + 1 + "," + year + "-" + numToString(month) + "-" + end;
			} else if (type == "day")
			{
				return year + "-" + numToString(month) + "-" + numToString(fromDay) + "," + year + "-" + numToString(month) + "-" + numToString(fromDay);
			} else 
			{
				return year + "-" + numToString(month) + "-" + numToString(fromDay) + "," + year + "-" + numToString(month) + "-" + numToString(endDay);
			}
		}
		
		/**
		 * 用于转化选择时间控件时，显示在label中的值
		 * */
		public static function formatDateToView(type:String, year:Number=NaN, month:Number=NaN, endMonth:Number=NaN, fromDay:Number=NaN, endDay:Number=NaN):String
		{
			var date:Date;
			var end:Number;
			if (type == "year")
			{
				date = new Date(year, 12);
				end = date.dateUTC;
				return year + "-0" + 1 + "-0" + 1 + "," + year + "-" + 12 + "-" + end;
			} else if (type == "month")
			{
				date = new Date(year, month);
				end = date.dateUTC;
				return year + "-" + numToString(month) + "-" + 1 + "," + year + "-" + numToString(month) + "-" + end;
			} else if (type == "day")
			{
				return year + "-" + numToString(month) + "-" + numToString(fromDay);
			} else 
			{
				return numToString(month) + "/" + numToString(fromDay) + "-" +  numToString(endMonth) + "/" + numToString(endDay);
			}
		}
		
		
		/**
		 * 用于转化响应的时间值，便于在label中显示
		 * */
		public static function convertString(type:String, dateString:String):String
		{
			var date:Date;
			var end:Number;
			var year:Number;
			var month:Number;
			if (type == "year")
			{
				date = new Date(int(dateString), 12);
				end = date.dateUTC;
				return year + "-0" + 1 + "-0" + 1 + "," + year + "-" + 12 + "-" + end;
			} else if (type == "month")
			{
				year = int(dateString.substr(0, 4));
				month = int(dateString.substr(dateString.indexOf("-") + 1, 2));
				date = new Date(year, month);
				end = date.dateUTC;
				return year + "-" +  numToString(month) + "-" + 1 + "," + year + "-" +  numToString(month) + "-" + end;
			}  else if (type == "day")
			{
				return dateString + "," + dateString;
			} else 
			{
				return dateString;
			}
		}
		
		public static function numToString(value:Number):String
		{
			if (value < 10)
			{
				return "0"+value;
			} else 
			{
				return value.toString();
			}
			
		}
		
		public static function dateTypeToCH(type:String):String
		{
			var value:String;
			switch (type)
			{
				case "day":
				case "日":
					return "日";
				case "week":
				case "周":
					return "周";
				case "month":
				case "月":
					return "月";
				case "year":
				case "年":
					return "年";
			}
			return null;
		}
		
		public static function dateTypeToEH(type:String):String
		{
			var value:String;
			switch (type)
			{
				case "day":
				case "日":
					return "day";
				case "week":
				case "周":
					return "week";
				case "month":
				case "月":
					return "month";
				case "year":
				case "年":
					return "year";
			}
			return null;
		}
		
		public static function getDateString():String
		{
			var date:Date = new Date();
			return date.fullYear + numToString(date.month + 1) + numToString(date.date);
		}
	}
}